<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 神奇字符串 -->
    <script>
      var magicalString = function (n) {
        let s = [1, 2, 2]
        for (let i = 2; s.length < n; i++) {
          let last = s[s.length - 1]
          for (let j = 0; j < s[i]; j++) {
            s.push(last ^ 3)
          }
        }
        return s.slice(0, n).filter(x => x === 1).length
      }
      console.log(magicalString(6))
    </script>
  </body>
</html>
